package com.shanghaionstar.activity.webview.handler;

import android.content.Context;
import android.location.Location;
import android.os.Message;
import android.webkit.WebView;

import com.onstar.cn.common.util.MyPost;
import com.shanghaionstar.R;
import com.shanghaionstar.utils.AlertDialogUtil;
import com.shanghaionstar.utils.MyLocationUtil;

import java.util.Map;

/**
 * Created by Rengui Lu on 2017/3/3.
 */
public class KuaiDaDiWebHandler extends DefaultWebHandler {

    public KuaiDaDiWebHandler(Context context) {
        super(context);
    }

    @Override
    public void handMsg(Message msg) {
        switch (msg.what) {
            case IWebType.TYPE_LOCATION:
                requestLocation(msg);
                break;
            case IWebType.TYPE_ON_OVERRIDE_URL:
                onPageStart(msg);
                break;
        }
    }

    private void requestLocation(Message msg) {
        CommonWebViewActivity mActivity;
        if (context instanceof CommonWebViewActivity) {
            mActivity = (CommonWebViewActivity) context;
            WebView webview = mActivity.getWebview();
            String linkUrl = ((WebDataEntity) msg.obj).getLinkUrl();
            Map<String, String> header = mActivity.getExtraHeaders();
            requestLocation(webview, linkUrl, header);
        }
    }

    private void onPageStart(Message msg) {
        CommonWebViewActivity mActivity;
        if (context instanceof CommonWebViewActivity) {
            mActivity = (CommonWebViewActivity) context;
            WebView webview = mActivity.getWebview();
            String linkUrl = msg.obj.toString();
            webview.loadUrl(linkUrl);
        }
    }

    public void requestLocation(final WebView webview, final String linkUrl, final Map<String, String> header) {
        MyLocationUtil.LocationResult locationResult = new MyLocationUtil.LocationResult() {
            @Override
            public void gotLocation(final Location location) {
                if (location != null) {
                    MyPost.post(new Runnable() {
                        @Override
                        public void run() {
                            webview.stopLoading();
                            String url = linkUrl + "&lat=" + location.getLatitude() + "&lng=" + location.getLongitude();
                            //滴滴代驾需要加特殊处理
                            webview.loadUrl(url, header);
                        }
                    });

                } else {
                    MyPost.post(new Runnable() {
                        @Override
                        public void run() {
                            AlertDialogUtil.showAlertDialog(context, R.string.dialog_warning,
                                    R.string.poi_no_gps_signal);
                        }
                    });
                }
            }
        };
        MyLocationUtil myLocationUtil = new MyLocationUtil(context, locationResult);
        myLocationUtil.initMyLocation();
    }

}
